package com.appspot.didit.client.ui;

import com.appspot.didit.client.workitem.ChangesListener;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.TextBox;

public class EnhancedTextBox extends TextBox {
	
	ChangesListener cl;
	String oldValue;
	int rowCount;
	/*PopupPanel tip;
	
	public PopupPanel getTip() {
		return tip;
	}

	public void setTip(PopupPanel tip) {
		this.tip = tip;
	}*/

	public EnhancedTextBox() {
		super();
		//tip = new PopupPanel();
		//tip.hide();
		this.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				setStyleName("");
				setOldValue(getValue());
			}
		});
		this.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				if(oldValue == null || !getValue().equals(oldValue)) {	
					System.out.println("updating");
					update();
					//hm.fireEvent(new WorkitemChangedEvent(rowCount));
				}
				if(getValue() != null) {// && !getValue().equals("")) {
					setStyleName("tb-noborder");
				}
			}
		});
		/*this.addMouseOverHandler(new MouseOverHandler() {
			public void onMouseOver(MouseOverEvent event) {
				// TODO Auto-generated method stub
				tip.add(new HTML(getValue()));
				tip.setPopupPosition(getAbsoluteLeft(), getAbsoluteTop());
				tip.show();
			} 
			
		});
		
		addMouseOutHandler(new MouseOutHandler() {
			public void onMouseOut(MouseOutEvent event) {
				// TODO Auto-generated method stub
				tip.hide();
			}
	    });*/
	}
	
	public EnhancedTextBox(String value) {
		this();
		setValue(value);
		rowCount = 0;
	}
	
	public EnhancedTextBox(String value, int rowCount) {
		this();
		setValue(value);
		this.rowCount = rowCount;
	}
	
	
	public ChangesListener getListener() {
		return cl;
	}

	public void setListener(ChangesListener cl) {
		this.cl = cl;
	}
	
	public String getOldValue() {
		return oldValue;
	}

	public void setOldValue(String oldValue) {
		this.oldValue = oldValue;
	}
	
	public void update(){
		if(rowCount != 0) {
			//WorkitemManager.getInstance().update(rowCount);
			cl.update(rowCount);
		}
	}
}
